{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "53b9049f",
   "metadata": {},
   "source": [
    "# requests(让HTTPS服务人类)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e2125043",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7880eb2",
   "metadata": {},
   "source": [
    "* r 是指 response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "320c8f63",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1297eaef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c84936f",
   "metadata": {},
   "source": [
    "* r.content\n",
    "> 1. 可以获取请求数据内容（观察页面数据形态 HTML,Json）\n",
    "> 2. b 是指 bytes :二进制文件\n",
    "* r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "048c9258",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"http://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "301b4d1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e8ec3e6c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.25.1\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62307a47-7520699e24f4f99124c1bb16\"\\n  }, \\n  \"origin\": \"120.85.41.250\", \\n  \"url\": \"http://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9ed135dd",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62307a47-7520699e24f4f99124c1bb16'},\n",
       " 'origin': '120.85.41.250',\n",
       " 'url': 'http://httpbin.org/get'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json() #将json 转换成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "be2479ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.25.1\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62307a47-7520699e24f4f99124c1bb16\"\\n  }, \\n  \"origin\": \"120.85.41.250\", \\n  \"url\": \"http://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c78d2a39",
   "metadata": {},
   "source": [
    "* 3.payload                         \n",
    "                      使用payload 来实现请求数据传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c8fb33dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'name':'zhichao',\n",
    "    'JOB':'IT'\n",
    "}\n",
    "r = requests.get(\"https://httpbin.org/get\",params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "eee103b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a814c214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'JOB': 'IT', 'name': 'zhichao'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.25.1',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62307a48-6934c9af25a544c3376962b9'},\n",
       " 'origin': '120.85.41.250',\n",
       " 'url': 'https://httpbin.org/get?name=zhichao&JOB=IT'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "085e908e",
   "metadata": {},
   "source": [
    "# 2. face++ detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d0823201",
   "metadata": {},
   "outputs": [],
   "source": [
    "#1. 准备base url （对应该功能的url链接）\n",
    "face_detect_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "    \n",
    "#2. 准备参数数据\n",
    "payload ={\n",
    "    'api_key':'SMVJThrY1hoB4CDuwbI9kF1aBaZE7TNa',\n",
    "    'api_secret':'Rbd67H3brlaPlhwaTgx8vwqkAop0ljBm',\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "    'return_lamdmark': 1,\n",
    "    'return_attributes':'gender,age,smile,emotion'\n",
    "}\n",
    "\n",
    "#3. 检查调用的请求方法：post\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2ae5dcae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "398eb019",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647344201,c496099a-4ebd-4c51-8caa-542ec6b2de20',\n",
       " 'time_used': 6,\n",
       " 'error_message': 'BAD_ARGUMENTS: return_attributes'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6eb4aff6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
